from typing import List, Optional
from pydantic_settings import BaseSettings
from pydantic import AnyHttpUrl, ConfigDict
import os
import platform
import logging

class Settings(BaseSettings):
    # API 配置
    PROJECT_NAME: str = "Travel Agent API"
    API_V1_STR: str = "/api"
    SECRET_KEY: str = "your-secret-key-here"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8

    # CORS 配置
    BACKEND_CORS_ORIGINS: List[str] = [
        "http://localhost:5000",
        "http://localhost:5001",
        "http://127.0.0.1:5000",
        "http://127.0.0.1:5001",
        "http://erp.hkpublicholiday.com",
        "http://erpadmin.hkpublicholiday.com",
        "https://erp.hkpublicholiday.com",
        "https://erpadmin.hkpublicholiday.com"
    ]
    
    # 数据库配置
    DB_HOST: str = None  # 移除默认值
    DB_USER: str = "ljy"
    DB_PASSWORD: str = "Lijinyuan,.1983"
    DB_NAME: str = "travel_agent"
    DB_CHARSET: str = "utf8mb4"

    # PostgreSQL配置（可选）
    POSTGRES_SERVER: Optional[str] = None
    POSTGRES_USER: Optional[str] = None
    POSTGRES_PASSWORD: Optional[str] = None
    POSTGRES_DB: Optional[str] = None
    DATABASE_URL: Optional[str] = None

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 在初始化时设置数据库主机
        system = platform.system()
        logging.info(f"Current system: {system}")  # 添加日志
        self.DB_HOST = "120.78.7.85" if system == "Linux" else "172.28.233.235"
        logging.info(f"Selected DB_HOST: {self.DB_HOST}")  # 添加日志

    @property
    def DATABASE_URL(self):
        # Windows 为开发环境，Linux 为生产环境
        if platform.system() == "Linux":
            return f"mysql+pymysql://ljy:Lijinyuan,.1983@120.78.7.85:3306/travel_agent?charset=utf8mb4"
        return f"mysql+pymysql://ljy:Lijinyuan,.1983@172.28.233.235:3306/travel_agent?charset=utf8mb4"

    @property
    def SQLALCHEMY_DATABASE_URI(self) -> str:
        uri = f"mysql+pymysql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:3306/{self.DB_NAME}?charset={self.DB_CHARSET}"
        logging.info(f"Database URI: {uri}")  # 添加日志
        return uri

    model_config = ConfigDict(
        case_sensitive=True,
        env_file=".env",
        extra="allow"  # 允许额外的字段
    )

settings = Settings() 